function shoppingTrolley() {
    var aggregate = 0;
    $(function () {
        $.ajax({
            url: "cartServlet?user_name="+document.getElementById("top1_login").innerText,
            type: "get",
            dataType: "json",
            success: function (res) {
                var shopping_tbody = $("#shopping_tbody");
                shopping_tbody.empty();
                for (var i = 0; i < res.length; i++) {
                    var tr = document.createElement("tr");

                    var td1 = document.createElement("td");
                    td1.setAttribute("height","35px");
                    td1.innerText = i+1;

                    var td2 = document.createElement("td");
                    td2.innerText = res[i].productName;

                    var td3 = document.createElement("td");
                    td3.innerText = res[i].price;

                    var td4 = document.createElement("td");

                    var input_1 = document.createElement("input");
                    input_1.setAttribute("type","button");
                    input_1.setAttribute("id","j"+res[i].productName);
                    input_1.setAttribute("value","-");
                    input_1.setAttribute("onclick","reduce(id)");

                    var input_2 = document.createElement("input");
                    input_2.style.width = "30px";
                    input_2.style.textAlign = "center";
                    input_2.setAttribute("id","amount"+i)
                    input_2.setAttribute("type","text");
                    input_2.setAttribute("value",res[i].quantity);

                    var input_3 = document.createElement("input");
                    input_3.setAttribute("type","button");
                    input_3.setAttribute("id","i"+res[i].productName);
                    input_3.setAttribute("value","+");
                    input_3.setAttribute("onclick","increase(id)");

                    td4.appendChild(input_1);
                    td4.appendChild(input_2);
                    td4.appendChild(input_3);

                    var td5 = document.createElement("td");
                    td5.setAttribute("name","subtotal");
                    td5.innerText = ("￥"+ (res[i].price*res[i].quantity));

                    var td6 = document.createElement("td");
                    var input_4 = document.createElement("input");
                    input_4.setAttribute("id",res[i].productName);
                    input_4.setAttribute("type","button");
                    input_4.setAttribute("value","删除");
                    input_4.setAttribute("onclick","Delete_shopping(id)");

                    td6.appendChild(input_4);

                    tr.appendChild(td1);
                    tr.appendChild(td2);
                    tr.appendChild(td3);
                    tr.appendChild(td4);
                    tr.appendChild(td5);
                    tr.appendChild(td6);
                    shopping_tbody.append(tr);

                    aggregate += res[i].price*res[i].quantity;
                }
                document.getElementById("aggregate").innerText = aggregate;
            },
            error: function (res) {
                alert("所有购物车获取失败！")
            }
        });
    });
}

//购物车的点击事件
function shopping_click() {
    var register =  document.getElementById("top2_a").getAttribute("name");
    // alert(register);
    if (register.trim().length==0){
        window.location.href = "/MyXiaomiShop/login.jsp";
    }else {
        window.location.href = "/MyXiaomiShop/cart.jsp";
    }
}

//增加商品数量
function increase(e) {
    // alert(e.substring(1,e.length));
    var amount = document.getElementById(e).previousSibling;
    amount.value = (parseInt(amount.value)+1);

    $(function () {
        $.ajax({
            url: "cart_reviseServlet?productName=" + e.substring(1,e.length) + "&amount=" + amount.value,
            type: "get",
            dataType: "json",
            success: function (res) {
                if (res){
                    console.log(" 修改成功");
                }
            },
            error: function (res) {
                alert("所有购物车获取失败！")
            }
        });
    });

    var fu = document.getElementById(e).parentNode;
    var price = fu.previousSibling;

    var subtotal = fu.nextSibling;

    subtotal.innerText = "￥"+(parseInt(price.innerText)*parseInt(amount.value));

    var subtotals = document.getElementsByName("subtotal");
    var aggregates = 0;
    for (var i = 0; i < subtotals.length; i++) {
        var subtotalss = subtotals[i].innerText.substring(1,subtotals[i].innerText.length);
        aggregates = (parseInt(aggregates) + parseInt(subtotalss));
    }
    var aggregate = document.getElementById("aggregate");
    // alert(aggregate.innerText);
    aggregate.innerText = aggregates;
}

//减少商品数量
function reduce(e) {
    // alert(e.substring(1,e.length));
    var amount = document.getElementById(e).nextSibling;
    if (parseInt(amount.value)<=1){
        return;
    }else {
        amount.value = amount.value-1;

        $(function () {
            $.ajax({
                url: "cart_reviseServlet?productName=" + e.substring(1,e.length) + "&amount=" + amount.value,
                type: "get",
                dataType: "json",
                success: function (res) {
                    if (res){
                        console.log(" 修改成功");
                    }
                },
                error: function (res) {
                    alert("所有购物车获取失败！")
                }
            });
        });

        var fu = document.getElementById(e).parentNode;

        var price = fu.previousSibling;

        var subtotal = fu.nextSibling;

        subtotal.innerText  = "￥"+(parseInt(price.innerText)*parseInt(amount.value));

        var subtotals = document.getElementsByName("subtotal");

        var aggregates = 0;
        for (var i = 0; i < subtotals.length; i++) {
            var subtotalss = subtotals[i].innerText.substring(1,subtotals[i].innerText.length);
            aggregates = (parseInt(aggregates) + parseInt(subtotalss));
        }
        var aggregate = document.getElementById("aggregate");
        // alert(aggregate.innerText);
        aggregate.innerText = aggregates;
    }
}

//删除当前商品列的信息
function Delete_shopping(e) {
    $(function () {
        $.ajax({
            url: "cart_DeleteServlet?productName=" + e + "&name=" + document.getElementById("top1_login").innerText,
            type: "get",
            dataType: "json",
            success: function (res) {
                if (res){
                    shoppingTrolley();
                    window.location.href = "/MyXiaomiShop/cart.jsp";
                }
            },
            error: function (res) {
                alert("所有购物车获取失败！")
            }
        });
    });
}

//清空购物车
function shoppin_empty() {
    $(function () {
        $.ajax({
            url: "cart_shoppin_empty_Servlet?&user_name=" + document.getElementById("top1_login").innerText,
            type: "get",
            dataType: "json",
            success: function (res) {
                if (res){
                    shoppingTrolley();
                    window.location.href = "/MyXiaomiShop/cart.jsp";
                }
            },
            error: function (res) {
                alert("所有购物车获取失败！")
            }
        });
    });
}

//添加订单
function shoppin_add() {
    window.location.href = "/MyXiaomiShop/order.jsp";
}